<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="类别" prop="">
            <tree-select
              :options="optionsTree"
              v-model="queryParams.categoryId"
              :searchable="false"
              :default-expand-level="Infinity"
              :open-on-click="true"
              :open-on-focus="true"
              :normalizer="normalizer"
              placeholder="请选择类别"
              @keyup.enter.native="handleQuery"
              style="width: 240px"
            />
          </el-form-item>

          <el-form-item el-form-item label="条码" prop="barCode">
            <el-input
              v-model="queryParams.barCode"
              placeholder="请输入条码查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入名称查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="规格" prop="safetyStock">
            <el-input
              v-model="queryParams.standard"
              placeholder="请输入规格查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="型号" prop="model">
            <el-input
              v-model="queryParams.model"
              placeholder="请输入型号查找"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['material:material:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['material:material:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['material:material:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['material:material:import']"
              >导入</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['material:material:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </el-row>
        <el-table
          v-loading="loading"
          :data="userList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column type="index" width="50" align="center" />
          <el-table-column
            label="条码"
            align="center"
            key="barCode"
            prop="barCode"
            v-if="columns[0].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="名称"
            align="center"
            key="name"
            prop="name"
            v-if="columns[1].visible"
          />
          <el-table-column
            label="规格"
            align="center"
            key="standard"
            prop="standard"
            v-if="columns[2].visible"
          />
          <el-table-column
            label="型号"
            align="center"
            key="model"
            prop="model"
            v-if="columns[3].visible"
          />
          <el-table-column
            label="颜色"
            align="center"
            key="color"
            prop="color"
            v-if="columns[4].visible"
          />
          <el-table-column
            label="类别"
            align="center"
            key="categoryName"
            prop="categoryName"
            v-if="columns[5].visible"
          />
          <el-table-column
            label="备注"
            align="center"
            key="remark"
            prop="remark"
            v-if="columns[6].visible"
          />
          <el-table-column
            label="单位"
            align="center"
            key="basicUnit"
            prop="basicUnit"
            v-if="columns[7].visible"
          />
          <el-table-column
            label="安全存量"
            align="center"
            key="safetyStock"
            prop="safetyStock"
            v-if="columns[8].visible"
          />
          <el-table-column
            label="库存"
            align="center"
            key="stock"
            prop="stock"
            v-if="columns[9].visible"
          />
          <el-table-column
            label="采购价"
            align="center"
            key="purchaseDecimal"
            prop="purchaseDecimal"
            v-if="columns[10].visible"
          />
          <el-table-column
            label="零售价"
            align="center"
            key="commodityDecimal"
            prop="commodityDecimal"
            v-if="columns[11].visible"
          />
          <el-table-column
            label="销售价"
            align="center"
            key="wholesaleDecimal"
            prop="wholesaleDecimal"
            v-if="columns[12].visible"
          />
          <el-table-column
            label="最低售价"
            align="center"
            key="lowDecimal"
            prop="lowDecimal"
            v-if="columns[13].visible"
          />
          <el-table-column
            label="状态"
            align="center"
            key="enable"
            prop="enable"
            v-if="columns[14].visible"
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.enabled == 1">启用</el-tag>
              <el-tag v-if="scope.row.enabled == 0">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="序列号"
            align="center"
            key="enableSerialNumber"
            prop="enableSerialNumber"
            v-if="columns[15].visible"
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.enableSerialNumber == 1">有</el-tag>
              <el-tag v-if="scope.row.enableSerialNumber == 0">无</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['material:material:edit']"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-tabs type="border-card">
          <el-tab-pane key="1" label="基本信息">
            <el-row class="form-row" :gutter="24">
              <el-col :lg="7" :md="10" :sm="20">
                <el-form-item label="名称" prop="name">
                  <el-input
                    v-model="form.name"
                    placeholder="请输入名称"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :lg="7" :md="10" :sm="20">
                <el-form-item label="规格" prop="standard">
                  <el-input
                    v-model="form.standard"
                    placeholder="请输入规格"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :lg="7" :md="10" :sm="20">
                <el-form-item label="单位" prop="basicUnit">
                  <el-input
                    v-model="form.basicUnit"
                    placeholder="请输入单位"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :lg="3" :md="6" :sm="16" style="padding-top: 8px;">
                  <!--:checked="unitChecked" @change="unitOnChange" -->
                  <el-checkbox >多单位</el-checkbox>
              </el-col>
            </el-row>
            <el-row class="form-row" :gutter="24">
              <el-col :lg="7" :md="12" :sm="24">
                <el-form-item label="型号" prop="model">
                  <el-input
                    v-model="form.model"
                    placeholder="请输入型号"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :lg="7" :md="12" :sm="24">
                <el-form-item label="颜色" prop="color">
                  <el-input
                    v-model="form.color"
                    placeholder="请输入颜色"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :lg="7" :md="12" :sm="24">
                <el-form-item label="类别" prop="">
                <tree-select
                  :options="optionsTree"
                  v-model="form.categoryId"
                  :searchable="false"
                  :default-expand-level="Infinity"
                  :open-on-click="true"
                  :open-on-focus="true"
                  :normalizer="normalizer"
                  placeholder="请选择类别"
                  @keyup.enter.native="handleQuery"
                  style="width: 100%;"
                />
              </el-form-item>
              </el-col>
            </el-row>
            <el-row class="form-row" :gutter="24">
              <el-col :lg="7" :md="12" :sm="24">
                <el-form-item label="序列号" prop="enableSerialNumber">
                  <el-select v-model="form.enableSerialNumber" filterable placeholder="请选择" style="width:100%">
                    <el-option
                      v-for="item in enableSerialNumberOptions"
                      :key="item.type"
                      :label="item.value"
                      :value="item.type">
                    </el-option>
                  </el-select>
              </el-form-item>
              </el-col>
              <el-col :lg="7" :md="12" :sm="24">
                <el-form-item label="安全存量" prop="safetyStock">
                  <el-input
                    v-model="form.safetyStock"
                    placeholder="请输入安全存量"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :lg="7" :md="12" :sm="24">
                <el-form-item label="多属性" prop="">
                  <el-switch
                  v-model="attrValues"
                  >
                </el-switch>
                </el-form-item>
              </el-col>
            </el-row>
             <el-card class="box-card" v-if="attrValues">
               <el-row :gutter="24">
                <el-col :lg="8" :md="12" :sm="24">
                  <el-form-item label="颜色" prop="manyColorName">
                    <el-select v-model="form.manyColorName" multiple placeholder="请选择">
                      <el-option
                        v-for="item in manyColorNameOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                     </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :lg="8" :md="12" :sm="24">
                    <el-form-item label="尺寸" prop="manySizeName">
                      <el-select v-model="form.manySizeName" multiple placeholder="请选择">
                        <el-option
                          v-for="item in manyColorNameOptions"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :lg="8" :md="12" :sm="24">
                    <el-form-item label="品牌" prop="other1Name">
                      <el-select v-model="form.other1Name" multiple placeholder="请选择">
                        <el-option
                          v-for="item in manyColorNameOptions"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :lg="8" :md="12" :sm="24">
                  <el-form-item label="口味" prop="other2Name">
                    <el-select v-model="form.other2Name" multiple placeholder="请选择">
                      <el-option
                        v-for="item in manyColorNameOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                     </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :lg="8" :md="12" :sm="24">
                  <el-form-item label="花纹" prop="other3Name">
                    <el-select v-model="form.other3Name" multiple placeholder="请选择">
                      <el-option
                        v-for="item in manyColorNameOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                     </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :lg="8" :md="12" :sm="24">
                    <el-form-item label="生成条码" prop="">
                      <el-switch
                      v-model="generateBarCode"
                      >
                    </el-switch>
                    </el-form-item>
                  </el-col>
              </el-row>
            </el-card>
             <el-row :gutter="10" class="mb8" style="padding-top: 15px">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAddMaterial"
                  v-hasPermi="['material:material:add']"
                  >新增</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDeleteMaterial"
                  v-hasPermi="['material:material:remove']"
                  >删除</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  @click="handleUpdate"
                  v-hasPermi="['material:material:edit']"
                  >采购价-批量</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  @click="handleDelete"
                  v-hasPermi="['material:material:edit']"
                  >零售价-批量</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="info"
                  plain
                  icon="el-icon-upload2"
                  size="mini"
                  @click="handleImport"
                  v-hasPermi="['material:material:edit']"
                  >销售价-批量</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="warning"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['material:material:edit']"
                  >最低售价-批量</el-button
                >
              </el-col>
            </el-row>
            <el-table
              v-loading="loading"
              :data="materialList"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column type="index" width="50" align="center" />
              <el-table-column
                label="条码"
                align="center"
                key="barCode"
                prop="barCode"
                v-if="columns[0].visible"
                :show-overflow-tooltip="true"
              >
              <template slot-scope="scope">
                <el-input v-model="scope.row.barCode"></el-input>
              </template>
              </el-table-column>  
              <el-table-column
                label="单位"
                align="center"
                key="basicUnit"
                prop="basicUnit"
                v-if="columns[7].visible"
              >
              <template slot-scope="scope">
                <el-input v-model="scope.row.basicUnit"></el-input>
              </template>
              </el-table-column>
              <el-table-column
                label="采购价"
                align="center"
                key="purchaseDecimal"
                prop="purchaseDecimal"
                v-if="columns[10].visible"
              >
              <template slot-scope="scope">
                <el-input v-model="scope.row.purchaseDecimal"></el-input>
              </template>
              </el-table-column>
              <el-table-column
                label="零售价"
                align="center"
                key="commodityDecimal"
                prop="commodityDecimal"
                v-if="columns[11].visible"
              >
              <template slot-scope="scope">
                <el-input v-model="scope.row.commodityDecimal"></el-input>
              </template>
              </el-table-column>
              <el-table-column
                label="销售价"
                align="center"
                key="wholesaleDecimal"
                prop="wholesaleDecimal"
                v-if="columns[12].visible"
              >
              <template slot-scope="scope">
                <el-input v-model="scope.row.wholesaleDecimal"></el-input>
              </template>
              </el-table-column>
              <el-table-column
                label="最低售价"
                align="center"
                key="lowDecimal"
                prop="lowDecimal"
                v-if="columns[13].visible"
              >
              <template slot-scope="scope">
                <el-input v-model="scope.row.lowDecimal"></el-input>
              </template>
              </el-table-column>
            </el-table>
            <el-row class="form-row" :gutter="24">
              <el-col :lg="24" :md="24" :sm="24">
                <el-form-item  label="备注" prop="remark">
                  <textarea :rows="2" placeholder="请输入备注" v-model="form.remark"  style="margin-top:8px;"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane key="2" label="扩展信息"> </el-tab-pane>
          <el-tab-pane key="3" label="初始库存"> </el-tab-pane>
          <el-tab-pane key="4" label="图片信息"> </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-file-module v-bind:upload="upload"></import-file-module>
  </div>
</template>
<script>
import { listCategory } from "@/api/material/category";
import { listMaterial } from "@/api/material/material";
import TreeSelect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";
import importFile from "@/components/import/index";
export default {
  name: "index",
  components: {
    TreeSelect,
    "import-file-module": importFile,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      attrValues: true,
      generateBarCode: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      optionsTree: [],
      // 用户表格数据
      userList: null,
      materialList: [{
        id: 0,
        barCode : 2333212133090
      }],
      materialIndex: 1,
      // 弹出层标题
      title: "",
      manyColorNameOptions: [{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
      enableSerialNumberOptions: [{
          value: '请选择',
          type: ''
        }, {
          value: '有',
          type: '1'
        }, {
          value: '无',
          type: '0'
        }],
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children,
        };
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/material/material/importData",
        // 模板下载地址
        downloadTemplate: "/material/material/importTemplate",
      },
      // 是否显示弹出层
      open: false,

      // 日期范围
      dateRange: [],

      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        barCode: undefined,
        name: undefined,
        categoryId: undefined,
        standard: undefined,
        model: undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `条码`, visible: true },
        { key: 1, label: `类型`, visible: true },
        { key: 2, label: `规格`, visible: true },
        { key: 3, label: `型号`, visible: true },
        { key: 4, label: `颜色`, visible: true },
        { key: 5, label: `类别`, visible: true },
        { key: 6, label: `备注`, visible: true },
        { key: 7, label: `单位`, visible: true },
        { key: 8, label: `安全存量`, visible: true },
        { key: 9, label: `库存`, visible: true },
        { key: 10, label: `采购价`, visible: true },
        { key: 11, label: `零售价`, visible: true },
        { key: 12, label: `销售价`, visible: true },
        { key: 13, label: `最低售价`, visible: true },
        { key: 14, label: `状态`, visible: true },
        { key: 15, label: `序列号`, visible: true },
      ],
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.loadNodeTree();
    this.getList();
  },
  methods: {
    loadNodeTree() {
      listCategory(0).then((response) => {
        this.optionsTree = response.data;
      });
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listMaterial(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.userList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      console.log(selection);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增商品信息";
    },
    handleAddMaterial(){
      this.materialList.push({
        id: this.materialList.length,
        barCode: 2333212133090+this.materialIndex++
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {},
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
          } else {
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除收支编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delInoutitem(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleDeleteMaterial(row){
      const ids = row.id || this.ids;
      if(ids.length <= 1){
        this.materialList.forEach((item,index)=> {
            if(item.id == ids){
              this.materialList.splice(index,1);
            }
        })

      }else{
        ids.forEach(id => {
          this.materialList.forEach((item,index)=> {
            if(item.id == id){
              this.materialList.splice(index,1);
            }
          })
        })
      }
      
      
    },
    unitOnChange(){
      console.log(1);
    },
    unitChecked(){
      console.log(2);
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "material/material/export",
        {
          ...this.queryParams,
        },
        `material_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="less">
.el-dialog {
  position: absolute;
  top: 43%;
  left: 50%;
  margin: 0 !important;
  transform: translate(-50%, -50%);
  height: 90%;
  max-width: calc(100% - 30px);
  display: flex;
  flex-direction: column;
}

.el-dialog__body {
  overflow: auto;
  height: 100%;
}
</style>